package com.westcom.vulb.activity;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;
import com.westcom.vulb.bean.Session;
import com.westcom.vulb.logic.SessionManager;
import com.westcom.vulb.logic.account;
import com.westcom.vulb.protocal.http.Response;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class ActivityEditProfile extends BasicActivity{
	
	private SessionManager sm;
	String userName, department, sex, birthday, career, address, homepage;
	EditText userNameText, departmentText, sexText, birthdayText, 
	careerText, addressText, homepageText;
	TextView back, done;
	ImageView profileImgView;
	OnClickListener listener;
	
	String profileImage;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_editprofile);
		sm = SessionManager.getInstance();
		userNameText = (EditText)findViewById(R.id.profile_name);
		departmentText = (EditText)findViewById(R.id.profile_department_content);
		sexText = (EditText)findViewById(R.id.profile_sex_content);
		birthdayText = (EditText)findViewById(R.id.profile_birthday_content);
		careerText = (EditText)findViewById(R.id.profile_career_content);
		addressText = (EditText)findViewById(R.id.profile_address_content);
		homepageText = (EditText)findViewById(R.id.profile_homepage_content);
		
		profileImage =  getIntent().getStringExtra("profileimg");
		profileImgView = (ImageView)findViewById(R.id.profile_image);
		profileImgView.setImageBitmap(account.Base64Str2Bitmap(profileImage));
		
		listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	switch(v.getId()){
            	case R.id.editprofile_header_right:{
            		UpdateProfileTask task = new UpdateProfileTask();
            		task.execute("");
				}
            	break;
				case R.id.editprofile_header_left:{
					finish();
				}
				break;
				default:{}
				break;
            	}
            }
        };

		back = (TextView)findViewById(R.id.editprofile_header_left);
		back.setOnClickListener(listener);
		done = (TextView)findViewById(R.id.editprofile_header_right);
		done.setOnClickListener(listener);
		
		GetProfileTask task = new GetProfileTask();
		task.execute("");
		
		
	}
	private void getData(HashMap<String, Object> data){
		userName = (String) data.get("userName");
		department = (String) data.get("department");
		sex = (String) data.get("sex"); 
		birthday = (String) data.get("birthday");  
		career = (String) data.get("career");  
		address = (String) data.get("address");   
		homepage = (String) data.get("url");
	}
	private HashMap<String, String> setData(){
		HashMap<String, String> profile = new HashMap<String, String>();
		profile.put("userName", userNameText.getText().toString());
		profile.put("department", departmentText.getText().toString());
		profile.put("sex", sexText.getText().toString());
		profile.put("birthday", birthdayText.getText().toString());
		profile.put("career", careerText.getText().toString());
		profile.put("address", addressText.getText().toString());
		profile.put("url", homepageText.getText().toString());
		return profile;
	}
    private class GetProfileTask extends AsyncTask<String, Integer, Integer> {
        @Override
        protected Integer doInBackground(String... params) {
        	Response profile_res;
        	account profileLogic = account.getInstanse();
        	Session session = sm.getCurrentSession();
        	profile_res = profileLogic.GetProfile(session.getSessionid(), session.getUid());
        	if(profile_res == null)
        		return null;
        	try
        	{
        		if(profile_res.errno == Response.SUCCESS)
            	{
        			HashMap<String, Object> data = profile_res.data;
        			getData(data);
            	}
        	}catch (Exception e) 
        	{
        		return null;
        	}
        	 return null;
        }
        
        @Override
        protected void onCancelled() {
            super.onCancelled();
            mLoadingDialog.hide();
        }
        
        @Override  
        protected void onPostExecute(Integer result) 
        {  
        	super.onPostExecute(result);
        	userNameText.setText(userName.toCharArray(),0,userName.length());
        	departmentText.setText(department.toCharArray(),0,department.length());
        	sexText.setText(sex.toCharArray(),0,sex.length()); 
        	birthdayText.setText(birthday.toCharArray(),0,birthday.length()); 
        	careerText.setText(career.toCharArray(),0,career.length());
        	addressText.setText(address.toCharArray(),0,address.length()); 
        	homepageText.setText(homepage.toCharArray(),0,homepage.length()); 
        	mLoadingDialog.hide();
        }
    }
    private class UpdateProfileTask extends AsyncTask<String, Integer, Integer> {
        @Override
        protected Integer doInBackground(String... params) {
        	Response profile_res;
        	account profileLogic = account.getInstanse();
        	Session session = sm.getCurrentSession();
        	
        	profile_res = profileLogic.UpdateProfile(session.getSessionid(), setData());
        	if(profile_res == null)
        		return null;
        	try
        	{
        		if(profile_res.errno == Response.SUCCESS)
            	{
            	}
        	}catch (Exception e) 
        	{
        		return null;
        	}
        	 return null;
        }
        
        @Override
        protected void onCancelled() {
            super.onCancelled();
            mLoadingDialog.hide();
        }
        
        @Override  
        protected void onPostExecute(Integer result) 
        {  
        	super.onPostExecute(result); 
        	mLoadingDialog.hide();
        	finish();
        }
    } 
}
